/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs.form;

import java.util.ArrayList;
import java.util.List;

import org.rails.extjs.Component;
import org.rails.extjs.Margins;
import org.rails.extjs.Option;

public class CompositeField extends Field {
	
	protected List<Component> items = new ArrayList<Component>();

	public CompositeField() {
		super();
	}

	public CompositeField(Option configOptions) {
		super(configOptions);
	}

	public CompositeField(String name) {
		super(name);
	}

	public CompositeField(String name, String fieldLabel) {
		super(name, fieldLabel);
	}

	public CompositeField(String name, String fieldLabel, Object value) {
		super(name, fieldLabel, value);
	}
	
	public void setLabelWidth(Integer labelWidth){
		addOption("labelWidth",labelWidth);
	}
	
	public Integer getLabelWidth(){
		return getInteger("labelWidth",100);
	}
	
	public void setCombineErrors(Boolean combineErrors){
		addOption("combineErrors",combineErrors);
	}
	
	public Boolean isCombineErrors(){
		return getBoolean("combineErrors",true);
	}
	
	public void setDefaultMargins(Margins defaultMargins){
		addOption("defaultMargins", defaultMargins);
	}
	
	public Margins getDefaultMargins(){
		return (Margins) getOption("defaultMargins");
	}
	
	public void setSkipLastItemMargin(Boolean skipLastItemMargin){
		addOption("skipLastItemMargin", skipLastItemMargin);
	}
	
	public Boolean isSkipLastItemMargin(){
		return getBoolean("skipLastItemMargin",true);
	}

	public void insertItem(int index,Component component){
		items.add(index, component);
	}

	public void addItem(Component component){
		items.add(component);		
	}
	
	public List<Component> getItems(){
		return items;
	}
	
	public void removeItem(int index){
		items.remove(index);
	}
	
	public Component getItem(int index){
		return items.get(index);
	}
	
	public void clearItems(){
		items.clear();
	}
	
	@Override
	protected void createItems(){
		super.createItems();
		if(items.size() > 0)
			addOption("items",items);
	}
	
	@Override
	public String getScript() {
		script = new StringBuffer("");
		createItems();
		script.append("new Ext.form.CompositeField(");
		script.append(createOptions());
		script.append(")");
		return script.toString();
	}

}
